package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.PigHouseDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPighouseDAO;

import cn.edu.scau.cmi.hombio.domain.PigHouse;
import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskPighouse;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for TaskPighouse entities
 * 
 */

@Service("TaskPighouseService")

@Transactional
public class TaskPighouseServiceImpl implements TaskPighouseService {

	/**
	 * DAO injected by Spring that manages PigHouse entities
	 * 
	 */
	@Autowired
	private PigHouseDAO pigHouseDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskPighouse entities
	 * 
	 */
	@Autowired
	private TaskPighouseDAO taskPighouseDAO;

	/**
	 * Instantiates a new TaskPighouseServiceImpl.
	 *
	 */
	public TaskPighouseServiceImpl() {
	}

	/**
	 * Delete an existing PigHouse entity
	 * 
	 */
	@Transactional
	public TaskPighouse deleteTaskPighousePigHouse(Integer taskpighouse_id, Integer related_pighouse_id) {
		TaskPighouse taskpighouse = taskPighouseDAO.findTaskPighouseByPrimaryKey(taskpighouse_id, -1, -1);
		PigHouse related_pighouse = pigHouseDAO.findPigHouseByPrimaryKey(related_pighouse_id, -1, -1);

		taskpighouse.setPigHouse(null);
		related_pighouse.getTaskPighouses().remove(taskpighouse);
		taskpighouse = taskPighouseDAO.store(taskpighouse);
		taskPighouseDAO.flush();

		related_pighouse = pigHouseDAO.store(related_pighouse);
		pigHouseDAO.flush();

		pigHouseDAO.remove(related_pighouse);
		pigHouseDAO.flush();

		return taskpighouse;
	}

	/**
	 * Delete an existing TaskPighouse entity
	 * 
	 */
	@Transactional
	public void deleteTaskPighouse(TaskPighouse taskpighouse) {
		taskPighouseDAO.remove(taskpighouse);
		taskPighouseDAO.flush();
	}

	/**
	 * Save an existing PigHouse entity
	 * 
	 */
	@Transactional
	public TaskPighouse saveTaskPighousePigHouse(Integer id, PigHouse related_pighouse) {
		TaskPighouse taskpighouse = taskPighouseDAO.findTaskPighouseByPrimaryKey(id, -1, -1);
		PigHouse existingpigHouse = pigHouseDAO.findPigHouseByPrimaryKey(related_pighouse.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigHouse != null) {
			existingpigHouse.setId(related_pighouse.getId());
			existingpigHouse.setNumber(related_pighouse.getNumber());
			existingpigHouse.setRfid(related_pighouse.getRfid());
			existingpigHouse.setMemo(related_pighouse.getMemo());
			related_pighouse = existingpigHouse;
		}

		taskpighouse.setPigHouse(related_pighouse);
		related_pighouse.getTaskPighouses().add(taskpighouse);
		taskpighouse = taskPighouseDAO.store(taskpighouse);
		taskPighouseDAO.flush();

		related_pighouse = pigHouseDAO.store(related_pighouse);
		pigHouseDAO.flush();

		return taskpighouse;
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public TaskPighouse deleteTaskPighouseTask(Integer taskpighouse_id, Integer related_task_id) {
		TaskPighouse taskpighouse = taskPighouseDAO.findTaskPighouseByPrimaryKey(taskpighouse_id, -1, -1);
		Task related_task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		taskpighouse.setTask(null);
		related_task.getTaskPighouses().remove(taskpighouse);
		taskpighouse = taskPighouseDAO.store(taskpighouse);
		taskPighouseDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		taskDAO.remove(related_task);
		taskDAO.flush();

		return taskpighouse;
	}

	/**
	 * Return a count of all TaskPighouse entity
	 * 
	 */
	@Transactional
	public Integer countTaskPighouses() {
		return ((Long) taskPighouseDAO.createQuerySingleResult("select count(o) from TaskPighouse o").getSingleResult()).intValue();
	}

	/**
	 */
	@Transactional
	public TaskPighouse findTaskPighouseByPrimaryKey(Integer id) {
		return taskPighouseDAO.findTaskPighouseByPrimaryKey(id);
	}

	/**
	 * Save an existing TaskPighouse entity
	 * 
	 */
	@Transactional
	public void saveTaskPighouse(TaskPighouse taskpighouse) {
		TaskPighouse existingTaskPighouse = taskPighouseDAO.findTaskPighouseByPrimaryKey(taskpighouse.getId());

		if (existingTaskPighouse != null) {
			if (existingTaskPighouse != taskpighouse) {
				existingTaskPighouse.setId(taskpighouse.getId());
				existingTaskPighouse.setMemo(taskpighouse.getMemo());
			}
			taskpighouse = taskPighouseDAO.store(existingTaskPighouse);
		} else {
			taskpighouse = taskPighouseDAO.store(taskpighouse);
		}
		taskPighouseDAO.flush();
	}

	/**
	 * Load an existing TaskPighouse entity
	 * 
	 */
	@Transactional
	public Set<TaskPighouse> loadTaskPighouses() {
		return taskPighouseDAO.findAllTaskPighouses();
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public TaskPighouse saveTaskPighouseTask(Integer id, Task related_task) {
		TaskPighouse taskpighouse = taskPighouseDAO.findTaskPighouseByPrimaryKey(id, -1, -1);
		Task existingtask = taskDAO.findTaskByPrimaryKey(related_task.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtask != null) {
			existingtask.setId(related_task.getId());
			existingtask.setTaskNumber(related_task.getTaskNumber());
			existingtask.setCreateTime(related_task.getCreateTime());
			existingtask.setStartTime(related_task.getStartTime());
			existingtask.setEndTime(related_task.getEndTime());
			existingtask.setMemo(related_task.getMemo());
			related_task = existingtask;
		}

		taskpighouse.setTask(related_task);
		related_task.getTaskPighouses().add(taskpighouse);
		taskpighouse = taskPighouseDAO.store(taskpighouse);
		taskPighouseDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		return taskpighouse;
	}

	/**
	 * Return all TaskPighouse entity
	 * 
	 */
	@Transactional
	public List<TaskPighouse> findAllTaskPighouses(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<TaskPighouse>(taskPighouseDAO.findAllTaskPighouses(startResult, maxRows));
	}
}
